package com.wennimei.util;

import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ShortUrlUtil {

    @SuppressWarnings("unchecked")
    public static String getShortUrl(String url) {
        if (null == url)
            return "";
        String api = "http://api.weibo.com/2/short_url/shorten.json?source=5786724301&url_long=%s";
        String json = HttpUtil.get(String.format(api, url));
        Gson gson = new Gson();
        Map<String, Object> map = gson.fromJson(json,
                new TypeToken<Map<String, Object>>() {}.getType());
        List<Map<String, String>> list = (List<Map<String, String>>) map
                .get("urls");
        if (null == list || list.isEmpty())
            return "";
        return list.get(0).get("url_short");
    }
}
